﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ITAP.ShprossesBuilder.Properties;
using System.Windows.Forms;
using System.IO;
using ITAP.ShprossesBuilder.Utils;
using System.Runtime.Serialization.Formatters.Binary;
using ITAP.ShprossesBuilder.Modelling.Components;
using ITAP.Core.Utils;

namespace ITAP.ShprossesBuilder.Operations
{
    public class OperationBinaryOpen : IOperation
    {
        #region IOperation Members

        public string Name
        {
            get { return "Открыть раскладку"; }
        }

        public OperationGroup OperationGroup
        {
            get { return OperationGroup.Other; }
        }

        public System.Drawing.Image Thumbnail
        {
            get { return Resources.Open.ToBitmap(); }
        }

        public void Execute(ModelControl modelControl)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Файл раскладки (*.spr)|*.spr|Все файлы|*.*";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                FileStream fileStream = File.Open(openFileDialog.FileName, FileMode.Open);
                Stream stream = new MemoryStream();
                Archiver.UnZip(fileStream, stream);
                
                BinaryFormatter binaryFormatter = new BinaryFormatter();
                object deserializedObject = binaryFormatter.Deserialize(stream);
                modelControl.ModelContainer = deserializedObject as ModelContainer;
                
                modelControl.SetCameraToCenter();
            }
        }

        public bool CanUndo
        {
            get { return false; }
        }

        #endregion
    }
}
